package com.stanwind.sgit.exec;

import static com.stanwind.sgit.GitCmdConstant.CMD_GIT_PULL;

import com.stanwind.sgit.exception.GitException;
import com.stanwind.sgit.util.StringUtils;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * GitOp
 *
 * @author : Stan
 * @version : 1.0
 * @date :  2019-11-29 14:45
 **/
public class GitPull extends AbsGitOperation {
    public static final Logger logger = LoggerFactory.getLogger(GitPull.class);

    public static final String STR_GIT_RES_SUCCESS = "Fast-forward";
    public static final String STR_GIT_RES_ALREADY_PULL = "Already up to date";

    @Override
    protected boolean processExecLog(String msg) throws GitException {
        logger.debug(msg);
        if (StringUtils.isBlank(msg)) {
            logger.error("get blank msg");
            return false;
        }

        if (!msg.contains(STR_GIT_RES_SUCCESS) && !msg.contains(STR_GIT_RES_ALREADY_PULL)) {
            logger.error("cannot get success msg ->" + msg);
            return false;
        }

        return true;
    }

    @Override
    public boolean exec(File localRepo) throws GitException {
        return execWithLogThread(CMD_GIT_PULL, localRepo);
    }

    public static void main(String[] args) throws GitException {
        File localRepo = new File("F:\\gittest\\B\\Base");
        GitPull op = new GitPull();
        op.exec(localRepo);
    }
}
